#!/bin/bash
# 部署应用到 Kubernetes 脚本

set -e

echo "=========================================="
echo "部署应用到 Kubernetes"
echo "=========================================="

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

# 检查 kubectl 是否可用
if ! command -v kubectl &> /dev/null; then
    echo "错误: kubectl 未安装或不在 PATH 中"
    exit 1
fi

cd "$PROJECT_ROOT"

# 初始化数据库
echo ""
echo "[1/4] 初始化数据库..."
echo "执行 schema.sql..."
kubectl run -it --rm mysql-init-schema --image=mysql:8.0 --restart=Never -- \
  mysql -h mysql -uroot -proot xy_portal < backend/XY_Portal/src/main/resources/db/schema.sql 2>/dev/null || echo "表结构已存在或已创建"

echo "执行 data.sql..."
kubectl run -it --rm mysql-init-data --image=mysql:8.0 --restart=Never -- \
  mysql -h mysql -uroot -proot xy_portal < backend/XY_Portal/src/main/resources/db/data.sql 2>/dev/null || echo "初始数据已插入"

echo "✓ 数据库初始化完成"

# 构建初始化 SQL 作为 ConfigMap
echo ""
echo "[2/4] 创建数据库初始化 ConfigMap..."
kubectl create configmap db-init-scripts \
  --from-file=schema.sql=backend/XY_Portal/src/main/resources/db/schema.sql \
  --from-file=data.sql=backend/XY_Portal/src/main/resources/db/data.sql \
  --dry-run=client -o yaml | kubectl apply -f -

# 执行初始化 Job
cat <<EOF | kubectl apply -f -
apiVersion: batch/v1
kind: Job
metadata:
  name: db-init-job
spec:
  template:
    spec:
      containers:
      - name: mysql-init
        image: mysql:8.0
        command:
        - /bin/bash
        - -c
        - |
          echo "初始化数据库表结构..."
          mysql -h mysql -uroot -proot xy_portal < /scripts/schema.sql || echo "表结构已存在"
          echo "初始化数据..."
          mysql -h mysql -uroot -proot xy_portal < /scripts/data.sql || echo "数据已存在"
          echo "数据库初始化完成"
        volumeMounts:
        - name: scripts
          mountPath: /scripts
      volumes:
      - name: scripts
        configMap:
          name: db-init-scripts
      restartPolicy: OnFailure
  backoffLimit: 3
EOF

echo "等待数据库初始化完成..."
kubectl wait --for=condition=complete --timeout=60s job/db-init-job || echo "初始化作业可能已完成或失败"
echo "✓ 数据库初始化 Job 已执行"

# 部署后端
echo ""
echo "[3/4] 部署后端应用..."
kubectl apply -f k8s/app/backend/configmap.yaml
kubectl apply -f k8s/app/backend/secret.yaml
kubectl apply -f k8s/app/backend/deployment.yaml
kubectl apply -f k8s/app/backend/service.yaml
echo "✓ 后端部署完成"

# 部署前端
echo ""
echo "[4/4] 部署前端应用..."
kubectl apply -f k8s/app/frontend/configmap.yaml
kubectl apply -f k8s/app/frontend/deployment.yaml
kubectl apply -f k8s/app/frontend/service.yaml
echo "✓ 前端应用部署完成"

# 等待部署完成
echo ""
echo "等待应用启动..."
kubectl wait --for=condition=available --timeout=300s deployment/xy-portal-backend
kubectl wait --for=condition=available --timeout=300s deployment/xy-portal-frontend

echo ""
echo "=========================================="
echo "应用部署完成！"
echo "=========================================="
echo ""
echo "查看服务状态："
echo "  kubectl get pods -l 'app in (xy-portal-backend,xy-portal-frontend)'"
echo ""
echo "访问应用："
echo "  后端: http://<节点IP>:30080"
echo "  前端: http://<节点IP>:30081"
echo ""

